package com.bawei.week.park

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.entity.ParkBeanItem
import com.bawei.lib_base.mvvm.BaseActivity
import com.bawei.week.R
import com.bawei.week.databinding.ActivityModifyBinding
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/ParkModifyActivity")
@AndroidEntryPoint
class ParkModifyActivity : BaseActivity<ActivityModifyBinding,ParkViewModel>() {
    @Autowired
    @JvmField
    var parkId = 0
    lateinit var bean : ParkBeanItem
    override fun initView() {
        ARouter.getInstance().inject(this)

        binding.tvEdit.setOnClickListener {
            bean.spaceNumber = binding.parkAddCarNo.text.toString()
            bean.remarks = binding.parkAddRemark.text.toString()
            bean.areaName = binding.parkAddArea.getItemAtPosition(0).toString()
            bean.spaceType = binding.parkAddType.getItemAtPosition(0).toString()
            if (binding.parkAddRg.checkedRadioButtonId == R.id.rb_kx){
                bean.spaceStatus = "空闲"
            }else if (binding.parkAddRg.checkedRadioButtonId == R.id.rb_zy){
                bean.spaceStatus = "已占用"
            }else{
                bean.spaceStatus = "维护中"
            }
            bean.userName = binding.parkaddUser.text.toString()
            bean.licensePlate = binding.parkaddCarNum.text.toString()
            bean.remarks = binding.parkAddRemark.text.toString()
            vm.updatePark(parkId,bean)
        }
    }

    override fun initData() {
        vm.parkModifySuc.observe(this){
            bean = it

            binding.parkAddCarNo.setText(it.spaceNumber)

            val types = arrayOf("标准车位", "VIP车位")
            val typeAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item,types)
            typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            binding.parkAddType.adapter = typeAdapter

            if (it.spaceType == "标准车位"){
                binding.parkAddType.setSelection(0)
            }else{
                binding.parkAddType.setSelection(1)
            }

            val areas = mutableListOf<String>("A区停车场", "B区停车场")
            val areaAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, areas)
            areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            binding.parkAddArea.adapter = areaAdapter

            if (!areas.contains(it.areaName)){
                areas.add(0,it.areaName)
                binding.parkAddArea.setSelection(0)
            }else{
                val indexOf = areas.indexOf(it.areaName)
                binding.parkAddArea.setSelection(indexOf)
            }

            if (it.spaceStatus == "空闲"){
                binding.parkAddRg.check(R.id.rb_kx)
            }else if (it.spaceStatus == "已占用"){
                binding.parkAddRg.check(R.id.rb_zy)
            }else{
                binding.parkAddRg.check(R.id.rb_wh)
            }

            binding.parkaddUser.setText(it.userName)
            binding.parkaddCarNum.setText(it.licensePlate)
            binding.parkAddRemark.setText(it.remarks)
        }

        vm.parkModifySuc.observe(this){
            ToastUtils.showShort("修改成功")
            finish()
        }
    }
}